{extend name="public/base"/}

{block name="title"}添加权限节点{/block}
{block name="main"}

<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-body">
                <form action="#" method="post" class="row" id="add-form" name="add-form">
                    <div class="form-group col-md-12">
                        <label for="category_id">所属目录</label>
                        <div class="form-controls">
                            <select class="form-control" id="category_id" name="category_id">
                                {volist name="list_permission_category" id="vo"}
                                <option value="{$vo.id}">{$vo.name}</option>
                                {/volist}
                            </select>
                        </div>
                    </div>
                    <div class="form-group col-md-12">
                        <label for="name">节点名称</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="请输入节点名称" data-validate="required:请输入节点名称"/>
                    </div>
                    <div class="form-group col-md-12">
                        <label for="path">权限路径</label>
                        <input type="text" class="form-control" id="path" name="path" placeholder="module/controller/action" data-validate="required:请输入权限路径"/>
                    </div>
                    <div class="form-group col-md-12">
                        <label for="is_menu">是否为菜单</label>
                        <select class="form-control" id="is_menu" name="is_menu">
                            <option value="1">是</option>
                            <option value="0">否</option>
                        </select>
                    </div>
                    <div class="form-group col-md-12">
                        <label for="sort_num">排序码</label>
                        <input type="text" class="form-control" id="sort_num" name="sort_num" placeholder="数字越大越靠前" data-validate="required:请输入排序码"/>
                    </div>
                    <div class="form-group col-md-12">
                        <button type="button" id="btn" class="btn btn-primary ajax-post" target-form="add-form">确 定</button>
                        <button type="button" class="btn btn-default" onclick="location.href='/admin/permission/index';return false;">返 回</button>
                    </div>
                    <!--</form>-->
                </form>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="js"}
<script type="text/javascript">
    $(function () {
        $('#btn').click(function () {
            let category_id = $('#category_id').val();
            let name = $('#name').val();
            let path = $('#path').val();
            let is_menu = $('#is_menu').val();
            let sort_num = $('#sort_num').val();
            if (!name) {
                swal('请输入节点名称', '', 'warning');
                return false;
            }
            var re_path = /^\w+\/\w+\/\w+$/;
            if (!re_path.test(path)) {
                swal('请输入正确的权限路径', '', 'warning');
                return false;
            }
            $.post('/admin/permission/add',
                {category_id: category_id, name: name, path: path, is_menu: is_menu, sort_num: sort_num},
                function (res) {
                    console.log(res);
                    if (res.code === 1) {
                        swal(res.msg, '', 'success')
                            .then(() => {
                                location.reload();
                            });
                    } else {
                        swal(res.msg, '', 'error');
                    }
                }
            );

        });
    });
</script>
{/block}